//
//  TuPianViewController.swift
//  新闻
//
//  Created by 芮京 on 2018/12/10.
//  Copyright © 2018 芮京. All rights reserved.
//

import UIKit
import Alamofire
class TuPianViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    //网址
    let PICTURE_PATH = "http://c.m.163.com/photo/api/list/0096/54GI0096.json"
    var arr:NSArray=[]
    //表格
    var tab:UITableView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //标题
        self.navigationItem.title="图片"
        
        tabb()
        VideoData()
    }
    //请求
    func VideoData(){
        Alamofire.request(PICTURE_PATH, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            if(response.error == nil){
                print("请求成功")
                self.arr = (response.result.value as? NSArray)!
                print("++++++++++++++++++++++++++\(self.arr)")
                self.tab?.reloadData()
                
            }else{
                print("+++++++Z++++++请求失败\(String(describing: response.error))")
            }
            //解析网址
        }
    }
    //表格
    func tabb() {
        tab = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        tab?.delegate=self
        tab?.dataSource=self
        tab?.rowHeight=200
        self.tab?.register( UINib(nibName: "TupianTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
        self.view .addSubview(tab!)
    }
    //分行
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    //行内容
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell:TupianTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TupianTableViewCell
        let name:NSDictionary = arr[indexPath.row] as! NSDictionary
        let md:TupianModel = TupianModel()
        md.dicP=name
        //图片方法
        cell.Tupian(model: md)
        return cell
        
    }
    //跳转
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let jumt = JumpTViewController()
        let ns:NSDictionary = arr[indexPath.row] as! NSDictionary
        jumt.name = ns.object(forKey: "setname") as! String
        jumt.tdec = ns.object(forKey: "desc") as! String
        jumt.arr = (ns.object(forKey: "pics")) as! NSArray
        self.navigationController?.pushViewController(jumt, animated: true)
        
    }

}
